//
//  HWGiftBagCenterDetailCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/9/22.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGiftBagCenterDetailCell: UITableViewCell {
    var bgView:UIView!
    var nameLabel:UILabel!
    var contentLabel:UILabel!
    var rightButton:UIButton!
    @objc var progressBarBg:UIView!
    @objc var progressBar:YLProgressBar!
    var receiveLabel:UILabel!
    var timeLabel:UILabel!
    var model:HWGiftBagCenterDetailListModel? {
        didSet {
            nameLabel.text = model?.cardname
            contentLabel.text = model?.content
            progressBar.progress = CGFloat((100 - (model?.bfb ?? 0))) / 100.0
            receiveLabel.text = "剩余\(model?.bfb ?? 0)%"
            timeLabel.text = "礼包周期：\(model?.start_time ?? "")至\(model?.end_time ?? "")"
            if (model?.card.count ?? 0) > 0 {
                rightButton.setTitle("复制", for: .normal)
                rightButton.setTitleColor(.colorWithHexString(hexString: "#FFFFFF"), for: .normal)
                rightButton.isEnabled = true
                UIColor.gradientColor(view: rightButton, colors: [UIColor.colorWithHexString(hexString: "#FFB60A").cgColor,
                                                                  UIColor.colorWithHexString(hexString: "#F66A05").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
            } else {
                if (model?.shengyu ?? 0) < 1 {
                    rightButton.setTitle("已领完", for: .normal)
                    rightButton.setTitleColor(.colorWithHexString(hexString: "#CCCCCC"), for: .normal)
                    rightButton.backgroundColor = .colorWithHexString(hexString: "#EEEEEE")
                    rightButton.isEnabled = false
                } else {
                    rightButton.setTitle("领取", for: .normal)
                    rightButton.setTitleColor(.colorWithHexString(hexString: "#FFFFFF"), for: .normal)
                    rightButton.isEnabled = true
                    UIColor.gradientColor(view: rightButton, colors: [UIColor.colorWithHexString(hexString: "#FFB60A").cgColor,
                                                                      UIColor.colorWithHexString(hexString: "#F66A05").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
                }
            }
        }
    }
    var copyButtonClick:((_:String)->())?
    var receiveButtonClick:((_:String)->())?
    class func cellWithTableView(tableView:UITableView) -> (HWGiftBagCenterDetailCell) {
        let reUse:String = "HWGiftBagCenterDetailCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGiftBagCenterDetailCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGiftBagCenterDetailCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        bgView = UIView()
        bgView.backgroundColor = .colorWithHexString(hexString: "#FFF3EC")
        bgView.layer.cornerRadius = 9*widthScale
        contentView.addSubview(bgView)
        
        rightButton = UIButton()
        rightButton.frame = CGRect(x: 0, y: 0, width: 51*widthScale, height: 30*widthScale)
        rightButton.layer.cornerRadius = 15*widthScale
        rightButton.titleLabel?.font = .boldSystemFont(ofSize: 13*widthScale)
        rightButton.layer.masksToBounds = true
        rightButton.addTarget(self, action: #selector(rightButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(rightButton)
        UIColor.gradientColor(view: rightButton, colors: [UIColor.colorWithHexString(hexString: "#FFB60A").cgColor,
                                                          UIColor.colorWithHexString(hexString: "#F66A05").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
        
        nameLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 16*widthScale)
        bgView.addSubview(nameLabel)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#888888", font: 11*widthScale)
        bgView.addSubview(contentLabel)
        
        progressBarBg = UIView()
        progressBarBg.layer.cornerRadius = 2*widthScale
        progressBarBg.layer.masksToBounds = true
        progressBarBg.backgroundColor = .colorWithHexString(hexString: "#FFDCD5")
        bgView.addSubview(progressBarBg)
        
        progressBar = YLProgressBar()
        progressBar.indicatorTextDisplayMode = .none
        progressBar.progressTintColors = [UIColor.colorWithHexString(hexString: "#FF8168")]
        progressBar.trackTintColor = .clear
        progressBar.cornerRadius = 0
        progressBar.hideStripes = true
        progressBar.progress = 0
        progressBar.hideGloss = true
        progressBar.progressStretch = true
        progressBar.progressBarInset = 0
        progressBarBg.addSubview(progressBar)
        
        receiveLabel = HWLabel.createLabel(title: "", color: "#888888", font: 11*widthScale)
        bgView.addSubview(receiveLabel)
        
        timeLabel = HWLabel.createLabel(title: "", color: "#888888", font: 11*widthScale)
        bgView.addSubview(timeLabel)
    }
    
    func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.top.equalToSuperview()
            make.bottom.equalTo(-9*widthScale)
            make.right.equalTo(-18*widthScale)
        }
        
        rightButton.snp.makeConstraints { make in
            make.right.equalTo(-9*widthScale)
            make.width.equalTo(51*widthScale)
            make.height.equalTo(30*widthScale)
            make.top.equalTo(27*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.top.equalTo(9*widthScale)
            make.height.equalTo(23*widthScale)
            make.right.equalTo(rightButton.snp.left).offset(-10*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(5*widthScale)
            make.right.equalTo(rightButton.snp.left).offset(-10*widthScale)
        }
        
        progressBarBg.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(13*widthScale)
            make.height.equalTo(5*widthScale)
            make.width.equalTo(129*widthScale)
        }
        
        progressBar.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        receiveLabel.snp.makeConstraints { make in
            make.left.equalTo(progressBarBg.snp.right).offset(5*widthScale)
            make.centerY.equalTo(progressBarBg)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(progressBarBg.snp.bottom).offset(12*widthScale)
            make.height.equalTo(15*widthScale)
            make.bottom.equalTo(-9*widthScale)
        }
    }
    
    @objc func rightButtonClick(_:UIButton) -> Void {
        if (model?.card.count ?? 0) > 0 {
            self.copyButtonClick?(model?.card ?? "")
        } else {
            if (model?.shengyu ?? 0) < 1 {
                
            } else {
                self.receiveButtonClick?(model?.id ?? "")
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
